'''
Created on 23/09/2012

@author: Charles
'''
import unittest
from Structural_Model.StructuralModel import *

class StructuralModelTest(unittest.TestCase):


    def setUp(self):
        self.model = StructuralModel("Tests/StructuralModelTest.xml")
        self.modelLink = StructuralModel("Tests/StructuralModelLinkTest.xml")
        #self.modelSubset = StructuralModel("Tests/StructuralModelSubsetTest.xml")

    def tearDown(self):
        pass


    def testDataParser(self):
        pass
    """
        data = self.model.dataParser(stream)
        self.assertTrue(data == {"Open":"2034", "Closed":"5607", "Value":"23.04", "Volume":"34069"})
        self.assertTrue()
    """
    def testFindData(self):
        parser = self.model.dataParser("NothingxmlwhatxmlhtmlnewhtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        #print item.getId()
        test = self.model.getRoot().getSubtree().findId("test")
        self.assertTrue(test.getMapping() == "test", "Failure it returned {0}".format(test.getMapping()))
        #data = self.model.fixStream(item)
        self.assertTrue(test.getData() == "what", "Failure fixStream returned {0}".format(test.getData()))
        self.assertTrue(self.model.getStream() == "htmlnewhtmlNothing", "Failure we didn't remove the right section of the stream {0}".format(self.model.getStream()))
        item = self.model.findData(parser, self.model.getStream())
        new = self.model.getRoot().getSubtree().findId("new")
        self.assertTrue(new.getMapping() == "true", "Failure for second item in stream did not get correct field returned {0}".format(new.getMapping()))
        
    def testFindSubData(self):
        parser = self.model.dataParser("NothingxmlwhathtmlnewhtmlxmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        test = self.model.getRoot().getSubtree().findId("test")
        self.assertTrue(test.getMapping() == "test")
        #data = self.model.fixStream(item)
        self.assertTrue(test.getData() == "what", "Failure returned {0} instead of \"what\"".format(test.getData()))
        item = self.model.findData(parser, self.model.getStream())
        nothing = self.model.getRoot().getSubtree().findId("test").getSubtree().findId("nothing")
        self.assertTrue(nothing.getMapping() == "nothing")
        #data = self.model.fixStream(item)
        self.assertTrue(nothing.getData() == "new")
        
    def testFindNormalData(self):
        parser = self.model.dataParser("NothingxmlhowhtmlcathtmlxmlhtmlokayhtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        item = self.model.findData(parser, self.model.getStream())
        nothing = self.model.getRoot().getSubtree().findId("test").getSubtree().findId("nothing")
        test = self.model.getRoot().getSubtree().findId("test")
        new = self.model.getRoot().getSubtree().findId("new")
        self.assertTrue(new.getData() == "okay")
        self.assertTrue(test.getData() == "how")
        self.assertTrue(nothing.getData() == "cat")
        
    def testOrderFindData(self):
        parser = self.model.dataParser("NothingxmlhtmlcathtmlhowxmlhtmlokayhtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        item = self.model.findData(parser, self.model.getStream())
        nothing = self.model.getRoot().getSubtree().findId("test").getSubtree().findId("nothing")
        test = self.model.getRoot().getSubtree().findId("test")
        new = self.model.getRoot().getSubtree().findId("new")
        self.assertTrue(test.getData() == "how")
        self.assertTrue(nothing.getData() == "cat")
        self.assertTrue(new.getData() == "okay")
        
    def testCreateDict(self):
        parser = self.model.dataParser("NothingxmlhtmlcathtmlhowxmlhtmlokayhtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        item = self.model.findData(parser, self.model.getStream())
        self.model.createDict(self.model.getRoot())
        #print self.model.getDict()
        dict = {"test":"how", "nothing":"cat", "true":"okay", "you":""}
        self.assertTrue(self.model.getDict() == dict, "Failure expected {0} got {1}".format(dict, self.model.getDict()))
        
    def testMultipleRecords(self):
        parser = self.model.dataParser("NothingxmlhtmlcathtmlhowxmlhtmlokayhtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        item = self.model.findData(parser, self.model.getStream())
        self.model.createDict(self.model.getRoot())
        #print self.model.getDict()
        dict = {"test":"how", "nothing":"cat", "true":"okay", "you":""}
        self.assertTrue(self.model.getDict() == dict, "Failure expected {0} got {1}".format(dict, self.model.getDict()))
        parser = self.model.dataParser("NothingxmlhowxmlhtmlwhatsnexthtmlNothing")
        item = self.model.findData(parser, self.model.getStream())
        item = self.model.findData(parser, self.model.getStream())
        self.model.createDict(self.model.getRoot())
        #print self.model.getDict()
        dict = {"test":"how", "nothing":"", "true":"whatsnext", "you":""}
        self.assertTrue(self.model.getDict() == dict, "Failure expected {0} got {1}".format(dict, self.model.getDict()))
        
    def testOptionTag(self):
        parser = self.modelLink.dataParser("NothingxmlhowhtmlcatxtestsohowarewextesthtmlxmlNothing")
        item = self.modelLink.findData(parser, self.modelLink.getStream())
        item = self.modelLink.findData(parser, self.modelLink.getStream())
        self.modelLink.createDict(self.modelLink.getRoot())
        dict = {"test":"how", "nothing":"cat", "what":"sohowarewe", "true":""}
        self.assertTrue(self.modelLink.getDict() == dict, "Failure expected {0} got {1}".format(dict, self.modelLink.getDict()))
        
    def testTagSubset(self):
        parser = self.model.dataParser("NothinghtmlgotnewbikehtmlxhtmlhelloxhtmlNothing")
        self.model.findData(parser, self.model.getStream())
        self.model.findData(parser, self.model.getStream())
        self.model.createDict(self.model.getRoot())
        dict = {"test":"", "nothing":"", "true":"gotnewbike", "you":"hello"}
        self.assertTrue(self.model.getDict() == dict, "Failure expected {0} got {1}".format(dict, self.model.getDict()))
        modelSubset = StructuralModel("Tests/StructuralModelSubsetTest.xml")
        parser = modelSubset.dataParser("NothingxmltesthtmlonxhtmlnewxhtmlhtmlxmlNothing")
        modelSubset.findData(parser, modelSubset.getStream())
        modelSubset.createDict(modelSubset.getRoot())
        dict = {"test":"test", "nothing":"on", "you":"new", "true":""}
        self.assertTrue(modelSubset.getDict() == dict, "Failure expect {0} got {1}".format(dict, modelSubset.getDict()))
        
    def testCheckOptions(self):
        modelSubset = StructuralModel("Tests/StructuralModelSubsetTest.xml")
        parser = modelSubset.dataParser("NothingxmltesthtmlonxhtmlnewxhtmlhtmlxmlNothing")
        currentItem = modelSubset.getRoot().getSubtree().findId("test").getSubtree().findId("nothing")
        node = modelSubset.getRoot().getSubtree().findId("test")
        check, stream = modelSubset.checkOptions(node, currentItem, "howxhtmlwhatxhtmlhtml")
        expected = "howxhtmlwhatxhtml"
        self.assertTrue(check == "howxhtmlwhatxhtml", "Failure expect {0} got {1}".format(expected, check))

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()